"""用户信息等"""

from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.contrib.auth import hashers
from core.serializers import  UserReadonlySerializer

@api_view(['GET'])
def user_info(request):
    '''返回当前登录用户信息'''
    serializer = UserReadonlySerializer(request.user)
    return Response(serializer.data)

@api_view(['POST'])
def change_pass(request):
    '''修改密码'''
    user = request.user
    original_pass = request.data['originalPass']
    if not hashers.check_password(original_pass, user.password):
        return Response({'detail': '原始密码不正确'}, status=status.HTTP_406_NOT_ACCEPTABLE)
    new_pass = request.data['pass']
    hashed_pass = hashers.make_password(new_pass)
    user.password = hashed_pass
    user.save()
    return Response(status=status.HTTP_204_NO_CONTENT)

    